<template>
	<div class="dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" :close-on-click-modal="false" width="1000px">
			<el-form ref="formRef" :model="state.ruleForm" :rules="state.rules">
				<el-row class="mb20" :gutter="20">
					<el-col :span="12">
						<el-form-item label="商品名称">
							<el-input v-model="state.ruleForm.goods_title" disabled />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="商品售价" prop="sale_price">
							<el-input v-model="state.ruleForm.sale_price" disabled />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row class="mb20" :gutter="20">
					<el-col :span="12">
						<el-form-item label="可退款订单数" prop="total_num">
							<el-input v-model="state.ruleForm.total_num" disabled />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="可退款订单金额（含运费）" prop="pay_amt">
							<el-input v-model="state.ruleForm.pay_amt" disabled />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row class="mb20" :gutter="20">
					<el-col :span="24">
						<el-form-item label="退款原因" prop="reason">
							<el-input v-model="state.ruleForm.reason" type="textarea" :autosize="{ minRows: 2, maxRows: 6 }" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row class="mb20" :gutter="20">
					<el-col :span="24">
						<el-form-item label="退款附件" prop="images">
							<ImageUploadCOS v-model="state.ruleForm.images" :is-dialog-upload="state.dialog.isShowDialog" :limit="6" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<el-button @click="onCancel" size="default">撤销</el-button>
				<el-button type="primary" @click="onSubmit" :loading="state.dialog.submitLoading" size="default">申请退款</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { orderApi } from '/@/api';

let callback: Function = () => {};
const OrderApi = orderApi();
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '申请退款',
		submitLoading: false,
	},
	ruleForm: {
		order_detail_id: '',
		goods_title: '',
		sale_price: '',
		total_num: '',
		pay_amt: '',
		reason: '',
		type: 1,
		images: '',
	},
	rules: {
		reason: [{ required: true, message: '退款原因不能为空', trigger: ['blur'] }],
		// images: [{ required: true, message: '退款附件不能为空', trigger: ['blur'] }],
	},
});
const formRef = ref();

/********** 触发事件 **********/
// 取消
const onCancel = () => {
	state.dialog = {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	};
};
// 提交
const onSubmit = () => {
	formRef.value.validate((valid: boolean) => {
		if (!valid) return;
		state.dialog.submitLoading = true;
		_orderAfterApply(state.ruleForm);
	});
};

/********** 方法事件 **********/
// 打开
const openDialog = (row: any, fn: Function) => {
	state.dialog.isShowDialog = true;
	state.dialog.title = `【${row.order_no}】申请退款`;
	callback = fn;
	state.ruleForm = {
		order_detail_id: row.orders.id,
		goods_title: row.orders.goods_title,
		sale_price: row.orders.sale_price,
		total_num: row.orders.total_num,
		pay_amt: row.orders.sale_price,
		type: 1,
		reason: '',
		images: '',
	};
};

/********** 网络请求 **********/
// 发货前退款
const _orderAfterApply = (params: Object) => {
	OrderApi.orderAfterApply(params)
		.then(() => {
			ElMessage.success('操作成功');
			onCancel();
			callback();
		})
		.catch(() => (state.dialog.submitLoading = false));
};

/********** 方法向父组件暴露 **********/
defineExpose({ openDialog });
</script>
